package com.paishen.common.util;


import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by peng on 2015/12/14.
 */
public class DateUtil {
    /**
     * yyyy年MM月dd日 HH:mm:ss
     */
    public static final String yyyy_MM_dd_HH_mm_ss_ch = "yyyy\u5e74MM\u6708dd\u65e5 HH:mm:ss";
    public static final String yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";
    public static final String yyyy_MM_dd_HH_mm = "yyyy-MM-dd HH:mm";
    public static final String MM_dd_HH_mm = "MM-dd HH:mm";
    public static final String MM_dd = "MM-dd";
    public static final String yyyyMMddHHmm = "yyyyMMddHHmm";
    public static final String yyyyMMddHHmmss = "yyyyMMddHHmmss";
    public static final String yyyyMMddHHmmssSSS = "yyyyMMddHHmmssSSS";
    public static final String yyyyMMdd = "yyyyMMdd";

    private static final SimpleDateFormat sdf = new SimpleDateFormat();

    public static Date parse(String date, String pattern) {
        synchronized (sdf) {
            sdf.applyPattern(pattern);
            try {
                return sdf.parse(date);
            } catch (Exception e) {
                return null;
            }
        }
    }

    public static String format(Date date, String pattern) {
        synchronized (sdf) {
            sdf.applyPattern(pattern);
            return sdf.format(date);
        }
    }

    public static String convert(String string, String oldPattern, String newPattern) {
        try {
            return format(parse(string, oldPattern), newPattern);
        } catch (Exception e) {
            return null;
        }
    }

    /** 比较的是秒数*/
    public static boolean innerTime(String timeRange, int offsetSeconds) {
        try {
            String startTime = timeRange.split("-")[0].trim();
            String endTime = timeRange.split("-")[1].trim();
            long today = parse(format(new Date(), yyyyMMdd), yyyyMMdd).getTime();
            long s = (System.currentTimeMillis() - today) / 1000;

            long start = Integer.parseInt(startTime.split(":")[0]) * 3600 + Integer.parseInt(startTime.split(":")[1]) * 60;
            long end = Integer.parseInt(endTime.split(":")[0]) * 3600 + Integer.parseInt(endTime.split(":")[1]) * 60;
            if (s >= start  && s <= end + offsetSeconds) {
                return true;
            }
        } catch (Exception e) {
            LogUtil.e("DateUtil innerTime\n" + e.getMessage());
        }
        return false;
    }
}
